package com.lxinet.jeesns.modules.wechat.web.manage;

import com.lxinet.jeesns.core.dto.ResponseModel;
import com.lxinet.jeesns.modules.wechat.entity.WechatMeta;
import com.lxinet.jeesns.modules.wechat.entity.WechatReplyConfig;
import com.lxinet.jeesns.modules.wechat.service.impl.WechatMetaServiceImpl;
import com.lxinet.jeesns.modules.wechat.service.impl.WechatReplyConfigServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lzs on 2017/5/5.
 */
@Controller("wchatReplyConfigController")
@RequestMapping("/")
public class WechatReplyConfigController {

    private static final String MANAGE_FTL_PATH = "/manage/wechatreplyconfig";

    @Resource
    private WechatReplyConfigServiceImpl wechatReplyConfigService;

    @Resource
    private WechatMetaServiceImpl wechatMetaService;


    @RequestMapping("${managePath}/wechatReplyConfig/list")
    public String wechatReplyConfigList(Model model) {
        List<WechatReplyConfig> wechatReplyConfigList = wechatReplyConfigService.getAll();
        List<WechatMeta> list = wechatMetaService.getAll();
        model.addAttribute("wmlist",list);
        model.addAttribute("list", wechatReplyConfigList);
        return MANAGE_FTL_PATH + "/list";
    }

    @RequestMapping("${managePath}/wechatReplyConfig/add")
    public String add(Model model){
        List<WechatMeta> list = wechatMetaService.getAll();
        model.addAttribute("list",list);
        return MANAGE_FTL_PATH + "/add";
    }

    @RequestMapping("${managePath}/wechatReplyConfig/save")
    @ResponseBody
    public Object save(WechatReplyConfig wechatReplyConfig){

        WechatMeta wechatMeta =wechatMetaService.findByWXUIN(wechatReplyConfig.getWxuin());

        if(wechatReplyConfig == null ||wechatMeta ==null){
            return new ResponseModel(-2);
        }
        if(StringUtils.isEmpty(wechatReplyConfig.getWxuin())){
            return new ResponseModel(-1,"名称不能为空");
        }
        if(wechatReplyConfig.getFlag() == null){
            wechatReplyConfig.setFlag(0);
        }
        wechatReplyConfig.setNickname(wechatMeta.getUser().getString("NickName"));
        wechatReplyConfigService.save(wechatReplyConfig);
        return new ResponseModel(3,"保存成功");
    }

    @RequestMapping("${managePath}/wechatReplyConfig/edit/{id}")
    public String edit(@PathVariable("id") int id, Model model){
        WechatReplyConfig wechatReplyConfig = wechatReplyConfigService.findById(id);
        List<WechatMeta> list = wechatMetaService.getAll();
        model.addAttribute("list",list);
        model.addAttribute("wechatReplyConfig",wechatReplyConfig);
        return MANAGE_FTL_PATH + "/edit";
    }

    @RequestMapping("${managePath}/wechatReplyConfig/update")
    @ResponseBody
    public Object update(WechatReplyConfig wechatReplyConfig){
        WechatMeta wechatMeta =wechatMetaService.findByWXUIN(wechatReplyConfig.getWxuin());
        if(wechatReplyConfig == null||wechatMeta == null){
            return new ResponseModel(-2);
        }
        WechatReplyConfig wrc = wechatReplyConfigService.findById(wechatReplyConfig.getId());
        if(wrc == null){
            return new ResponseModel(-1,"栏目不存在");
        }
        if(StringUtils.isEmpty(wechatReplyConfig.getWxuin())){
            return new ResponseModel(-1,"名称不能为空");
        }
        wechatReplyConfig.setNickname(wechatMeta.getUser().getString("NickName"));
        int flag = wechatReplyConfigService.update(wechatReplyConfig);
        if (flag == 1){
            return new ResponseModel(3,"修改成功");
        }
        return new ResponseModel(-1,"修改失败");
    }


    @RequestMapping("${managePath}/wechatReplyConfig/delete/{id}")
    @ResponseBody
    public Object delete(@PathVariable("id") int id){
        ResponseModel response = wechatReplyConfigService.delete(id);
        return response;
    }
}
